package com.orhon.smartcampus.modules.systemctl.mutation;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.orhon.smartcampus.modules.systemctl.dao.ApisDao;
import com.orhon.smartcampus.modules.systemctl.entity.Apis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.systemctl.dao.MenusDao;
import com.orhon.smartcampus.modules.systemctl.dao.ModulesDao;
import com.orhon.smartcampus.modules.systemctl.entity.Menus;
import com.orhon.smartcampus.modules.systemctl.entity.Modules;
import com.orhon.smartcampus.modules.systemctl.gqlInputModel.GQLMenusInput;
import com.orhon.smartcampus.modules.systemctl.inputMapper.MenusInputMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddMenusMutation implements DataFetcher {

    @Autowired
    MenusDao menusDao;

    @Autowired
    ModulesDao modulesDao;

    @Autowired
    ApisDao apisDao;


    /**
     * 新增/修改菜单信息
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        GQLMenusInput input = MenusInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        Menus menus = null;

        if (input.getId() != null) {
            menus = menusDao.get(Menus.class, input.getId());
            menus.setUpdatedAt(new Date());
        } else {
            menus = new Menus();
            menus.setCreatedAt(new Date());
        }
        if(input.getTitle() != null){

            menus.setTitle(JSON.toJSONString(input.getTitle()));
        }
        if(input.getName() != null){

            menus.setName(input.getName());
        }
        if(input.getPath() != null){

            menus.setPath(input.getPath());
        }
        if(input.getComponent() != null){

            menus.setComponent(input.getComponent());
        }
        if(input.getModule_id() != null) {
            menus.setModules(modulesDao.get(Modules.class, input.getModule_id()));
        }
        if(input.getCategory() != null){

            menus.setCategory(input.getCategory());
        }
        if(input.getType() != null){

            menus.setType(input.getType());
        }
        if(input.getParent() != null) {
            menus.setMenus(menusDao.get(Menus.class, input.getParent()));
        }
        if(input.getApis()!=null){
            if (input.getApis().size()>0){
                List<Apis> apis = new ArrayList<>();
                input.getApis().forEach(api_id->{
                    try {
                        Apis newapis = apisDao.findById(api_id);
                        apis.add(newapis);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
                menus.setApis(apis);
            }
        }
        if(input.getLevel() != null){

            menus.setLevel(input.getLevel());
        }
        if(input.getMeta() != null){

            menus.setMeta(JSON.toJSONString(input.getMeta()));
        }
        if(input.getClients() != null){

            menus.setClients(JSON.toJSONString(input.getClients()));
        }
        if(input.getIcon() != null){

            menus.setIcon(input.getIcon());
        }
        if(input.getOrdered() != null){

            menus.setOrdered(input.getOrdered());
        }
        if(input.getStatus() != null){

            menus.setStatus(input.getStatus());
        }
        try{
            menusDao.saveOrUpdate(menus);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

